﻿using System;

namespace Jodell.Lua
{
    internal static class Extensions
    {
        public static UInt32 ParseUInt32(this string data)
        {
            if (data == null)
                throw new ArgumentNullException("data");
            UInt32 value;
            if (data.StartsWith("#x", StringComparison.InvariantCultureIgnoreCase) || data.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase))
            {
                data = data.Substring(2);
                value = Convert.ToUInt32(data, 16);
            }
            else
            {
                value = Convert.ToUInt32(data);
            }
            return value;
        }
        public static Int32 ParseInt32(this string data)
        {
            if (data == null)
                throw new ArgumentNullException("data");
            Int32 value;
            if (data.StartsWith("#x", StringComparison.InvariantCultureIgnoreCase) || data.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase))
            {
                data = data.Substring(2);
                value = Convert.ToInt32(data, 16);
            }
            else
            {
                value = Convert.ToInt32(data);
            }
            return value;
        }
        public static UInt16 ParseUInt16(this string data)
        {
            if (data == null)
                throw new ArgumentNullException("data");
            UInt16 value;
            if (data.StartsWith("#x", StringComparison.InvariantCultureIgnoreCase) || data.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase))
            {
                data = data.Substring(2);
                value = Convert.ToUInt16(data, 16);
            }
            else
            {
                value = Convert.ToUInt16(data);
            }
            return value;
        }
        public static byte ParseByte(this string data)
        {
            if (data == null)
                throw new ArgumentNullException("data");
            byte value;
            if (data.StartsWith("#x", StringComparison.InvariantCultureIgnoreCase) || data.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase))
            {
                data = data.Substring(2);
                value = Convert.ToByte(data, 16);
            }
            else
            {
                value = Convert.ToByte(data);
            }
            return value;
        }

        public static bool IsNumber(this string data)
        {
            if (data == null)
                throw new ArgumentNullException("data");

            if (data == "USINT" ||
                data == "SINT" ||
                data == "UINT" ||
                data == "INT" ||
                data == "UDINT" ||
                data == "DINT" ||
                data == "REAL")
            {
                return true;
            }
            return false;
        }
    }
}
